######################################################################
#
# wt::server package
#
######################################################################

package require wt::core
package require wt::util

namespace eval ::wt::server:: {
  namespace export handleErrors serverVars virtual requestTime \
    elapsedTime unixRequestTime workerInfo taskInfo taskNotices \
    serverLog logLevel sleep taskSettings atEnd
}

proc ::wt::server::handleErrors {} {
  set buf "<html>\n<head><title>Error</title></head>\n<body>\n"
  append buf "<h1>Error</h1>"

  set errors ""
  set notices [taskNotices]
  foreach notice [taskNotices] {
    foreach {level msg} $notice {}
    if {$level <= [logLevel code error]} {
      append errors "<p>[::wt::web::htmlEncode $msg]</p>" 
    }
  }

  if {$errors != ""} {
    append buf "<p>Sorry, an error occurred while processing this request:</p>"
    append buf "<p><ul><pre>"
    append buf $errors
    append buf "</ul></pre></p>"
  } else {
    append buf "<p>Sorry, an error occurred while processing this request.</p>"
  }

  append buf "<p>Date: "
  append buf [clock format [clock seconds] -format "%Y-%m-%d %T %Z"]
  append buf "</p>\n"

  puts $buf

  flush stdout
}

package provide wt::server 1
